<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\View;
class UserAuth
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $aid = session('user:uid');
        if (!$aid) {
            if ($request->ajax() || (isset($_REQUEST['_async']) && 1 == $_REQUEST['_async'])) {
                return output(80001, '', ['redirect' => route('user/login')]);
            } else {
                return redirect()->route('user/login');
            }
        } else {
            View::share('menus', get_my_menu('u'));
            View::share('console_bash_url', env('CONSOLE_URL'));
            return $next($request);
        }
    }
}
